<template>
    <div >
      <zh-header-toolbar>
        <zh-button v-if="tabName=='classInfoTab'" buttonType="add" @click="childClassAdd">子级新增</zh-button>
        <zh-button v-if="tabName=='classInfoTab'" buttonType="save" @click="baseClassAdd">根目录新增</zh-button>
        <zh-button v-if="tabName=='classInfoTab'" buttonType="del" @click="delClass">删除分类</zh-button>
        <zh-button v-if="tabName=='classInfoTab'" buttonType="save" @click="saveClass">保存分类</zh-button>
        <zh-button v-if="tabName=='appInfoTab'" buttonType="add" @click="addAppClass">授权应用新增</zh-button>
      </zh-header-toolbar>
      <zh-row>
        <zh-col :span="6">
            <file-class-tree ref="classTreeRef" @nodeClick="treeNodeClick" style="height:800px">
            </file-class-tree>
        </zh-col>
        <zh-col :span="18">
          <zh-card shadow="hover" title="文件分类信息">
            <el-tabs v-model="tabName" :before-leave="beforeLeave" :stretch="true" >
              <el-tab-pane label="文件分类基础信息" name="classInfoTab" style="height:500px">
                <file-class-info ref="classInfoRef" :displayFlag="false" >
                </file-class-info>
              </el-tab-pane>
              <el-tab-pane label="授权应用信息" name="appInfoTab" style="height:500px">
                <file-app-class ref="fileAppClassRef"></file-app-class>
              </el-tab-pane>
              <el-tab-pane label="绑定元数据信息" name="fieldInfoTab" >
                <div style="text-align: center">
                  <el-transfer  v-model="fieldValue" :data="fieldData"
                                filterable
                                :props="{
                                key: 'fieldId',
                                label: 'fieldName',
                              }"
                                :filter-method="appFilterMethod"
                                @change="fieldChange"
                                filter-placeholder="请输入需要搜索的元数据"
                                :titles="['未关联元数据', '已关联元数据']"
                  >
                  </el-transfer>
                </div>
              </el-tab-pane>
            </el-tabs>
          </zh-card>
        </zh-col>
      </zh-row>
      <zh-dialog
          ref="appClassDialog"
          v-model="state.appClassVisible"
          title="应用分类授权信息"
          width="30%"
          @confirm="saveAppClass">
        <file-app-class-info :displayFlag="false" ref="fileAppClassInfo">
        </file-app-class-info>
      </zh-dialog>
    </div>
</template>

<script lang="ts" setup name="fileClassIndex">
import FileClassTree from '@/views/fileCenter/fileClass/component/FileClassTree.vue';
import FileClassInfo from '@/views/fileCenter/fileClass/component/FileClassInfo.vue';
import FileAppClass from '@/views/fileCenter/fileClass/component/FileAppClass.vue';
import FileAppClassInfo from '@/views/fileCenter/fileClass/component/FileAppClassInfo.vue';


import {ElMessageBox} from "element-plus";

// ------------------变量区------------------------------
const queryTreeParam=ref({parentKey:''});
const classTreeRef=ref();
const classInfoRef=ref();
const fileAppClassRef=ref();
const fileAppClassInfo=ref();
const tabName = ref('classInfoTab');
const fieldValue=ref([]);
const fieldData=ref([]);



const state = reactive({
    classInfo:{
      fileClassCode: '', parentCode: null,  parentClassTitle: null, secrecyLevel: null
    },
    appClassVisible:false,
});

//------------------初始化方法-----------------
//调用方法获取元数据
$api.common.fileSend.post('fileFieldParam0001', {}).then((res) => {
  fieldData.value = res.result.list;
}).catch(() => {});


//--------------------------定义调用方法--------------------------------------------------
//元数据穿梭框过滤
const appFilterMethod = (query, item) => {
  return item.fieldName.includes(query)
}

//穿梭框的值发生改变
function fieldChange(e,direct,keys){
  //判断穿梭框的移动方向
  if("right"==direct){
    //右移表示添加关联关系
    let fileClassCode = classTreeRef.value.selectNode.key;
    let info={
      'fileClassCode':fileClassCode,
      'fieldIds':keys
    };
    $api.common.fileSend.post('fileFieldClass0007', info).then(() => {
      $messageBox.success('关联成功', '提示');
    }).catch(() => {});
  }else{
    //左移表示取消关联关系
    let fileClassCode = classTreeRef.value.selectNode.key;
    let info={
      'fileClassCode':fileClassCode,
      'fieldIds':keys
    };
    $api.common.fileSend.post('fileFieldClass0008', info).then(() => {
      $messageBox.success('取消关联成功', '提示');
    }).catch(() => {});
  }

  console.log(keys);
}
function childClassAdd(){
  //获取当前选中节点的数据
  if(classTreeRef.value.selectNode){
      classInfoReset();
      state.classInfo.parentCode=classTreeRef.value.selectNode.key;
      state.classInfo.parentClassTitle=classTreeRef.value.selectNode.title;
      //调用文件分类子组件进行数据初始化
    classInfoRef.value.setInitData(state.classInfo);
  }else{
    $messageBox.warning('请先在左边分类树中选择节点', '提示');
  }
}

function treeNodeClick(){
  classInfoReset();
  //获取当前树选中的节点
  state.classInfo.fileClassCode=classTreeRef.value.selectNode.key;
  //判断当前激活的tab页面
  if("classInfoTab"==tabName.value){
    //调用文件分类子组件进行数据初始化
    classInfoRef.value.setInitData(state.classInfo);
  }
  if("appInfoTab"==tabName.value){
    fileAppClassRef.value.setInitData({fileClassCode:classTreeRef.value.selectNode.key});
  }
  if("fieldInfoTab"==tabName.value){
    getFieldInfo();
  }
}

function classInfoReset(){
  state.classInfo={
    fileClassCode: '', parentCode: null,  parentClassTitle: null, secrecyLevel: null
  }
  classInfoRef.value.setInitData(state.classInfo);
}

function saveClass(){
  //获取分类信息进行保存
  let classInfo = classInfoRef.value.classInfo;
  //判断是否存在主键进行分类的修改和新增
  //对
  if(classInfo.fileClassCode){
    $api.common.fileSend.post('fileClassM0003', classInfo).then((res) => {
      classInfo.value = res.result;
      classTreeRef.value.refreshTree();
      classInfoReset();
      $messageBox.success('编辑成功', '提示');

    }).catch(() => {});
  }else{
    $api.common.fileSend.post('fileClassM0002', classInfo).then((res) => {
      classInfo.value = res.result;
      classTreeRef.value.refreshTree();
      classInfoReset();
      $messageBox.success('新增成功', '提示');
    }).catch(() => {});
  }
}

function delClass(){
  //获取当前选中的节点
  if(classTreeRef.value.selectNode){
    ElMessageBox.confirm(
        '将删除当前文件分类:'+classTreeRef.value.selectNode.title,
        '提示',
        {
          confirmButtonText: '继续',
          type: 'warning',
          showCancelButton: false
        }
    ).then(() => {
      //根据id删除当前文件接口分类
      $api.common.fileSend.post('fileClassM0004', {fileClassCode:classTreeRef.value.selectNode.key}).then(() => {
        $messageBox.success('删除成功', '提示');
        classTreeRef.value.refreshTree();
      }).catch(() => {});
    });
  }
}

function baseClassAdd(){
  classInfoReset();
  //调用文件分类子组件进行数据初始化
  classInfoRef.value.setInitData(state.classInfo);
}

function beforeLeave(activeName){
 if("classInfoTab"!=activeName){
    //判断是否选中了左侧的文件分类树
    if(classTreeRef.value.selectNode){
      //获取已授权的app应用列表
      if("appInfoTab"==activeName){
        //刷新已授权应用列表
        fileAppClassRef.value.setInitData({fileClassCode:classTreeRef.value.selectNode.key});
      }
      //获取已关联的元数据列表
      if("fieldInfoTab"==activeName){
        getFieldInfo();
      }
    }else{
      $messageBox.warning('请先选择需要操作的文件分类', '提示');
      return false
    }

  }
}

function getFieldInfo(){
  if(classTreeRef.value.selectNode){
    //调用方法获取元数据
    $api.common.fileSend.post('fileFieldClass0009', {fileClassCode:classTreeRef.value.selectNode.key}).then((res) => {
      let list = res.result.list;
      let keys=[];
      for (let index in list){
        if(!keys.includes(list[index].fieldId)){
          keys.push(list[index].fieldId);
        }
      }
      fieldValue.value=keys;
    }).catch(() => {});
  }

}
function addAppClass(){
  //获取选中的文件分类节点
  if(classTreeRef.value.selectNode){
    state.appClassVisible=true;
    let info={
      fileClassCode:classTreeRef.value.selectNode.key,
      fileClassTitle:classTreeRef.value.selectNode.title
    }
    nextTick(() => {
      fileAppClassInfo.value.setInitData(info);
    });

  }else{
    $messageBox.warning('请先选择需要操作的文件分类', '提示');
    return false
  }
}



//保存新增的应用文件分类授权
function saveAppClass(){
  //获取子页面的数据
  let appClassInfo = {appId:'',expireTime:0,fileClassCode:'',fileClassPath:'' };
  console.log(appClassInfo);
  appClassInfo.appId=fileAppClassInfo.value.appClassInfo.appId;
  appClassInfo.fileClassCode=fileAppClassInfo.value.appClassInfo.fileClassCode;
  appClassInfo.fileClassPath=fileAppClassInfo.value.appClassInfo.fileClassPath;
  //默认审核通过
  appClassInfo.authStatus = '1';
  let dateStr = fileAppClassInfo.value.appClassInfo.expireTime;
  var date=new Date(dateStr);
  appClassInfo.expireTime= date.getTime();
  $api.common.fileSend.post('fileAppClass0004', appClassInfo).then(() => {
    $messageBox.success('新增成功', '提示');
    //刷新已授权应用列表
    fileAppClassRef.value.queryRefresh();
    state.appClassVisible=false;
  }).catch(() => {});

}
</script>
<style>
.el-transfer-panel{
  width: 300px;
  height: 100%;
}
.el-transfer-panel__body{
  height:  446px;
  width: 300px;
}
.el-transfer-panel__filter{
  width: 90%
}

.el-transfer-panel__list.is-filterable{
  height:300px;
}
</style>
